package org.darkforest.common.services.impl;

import org.darkforest.common.services.BoothGoodsService;
import org.darkforest.common.services.GoodsService;
import org.darkforest.dao.mapper.GoodsCommonMapper;
import org.darkforest.dao.mapper.PBoothGoodsMapper;
import org.darkforest.model.Goods;
import org.darkforest.model.GoodsCommon;
import org.darkforest.model.PBoothGoods;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Libing on 2015/7/28.
 */
@Service
public class BoothGoodsServiceImpl implements BoothGoodsService {

    @Autowired
    private PBoothGoodsMapper pBoothGoodsMapper;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsCommonMapper goodsCommonMapper;

    /**
     * 添加推荐展位商品
     *
     * @param goods_id
     * @param storeId
     * @return
     */
    public AjaxReturnData addRecommondGoods(Integer goods_id, Integer recommondType, Integer sort, Integer storeId) throws Exception {

        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        try {
            List<PBoothGoods> pBoothGoodsList = pBoothGoodsMapper.selectRecommondGoodsByPage(new PBoothGoods(storeId, false));
//            if(pBoothGoodsList.size()<8){

                GoodsCommon goods = goodsCommonMapper.selectByPrimaryKey(goods_id);
                PBoothGoods pBoothGoods = new PBoothGoods();
                pBoothGoods.setStoreId(goods.getStoreId());
                pBoothGoods.setGcId(goods.getGcId());
                pBoothGoods.setGoodsId(goods.getGoodsCommonid());
                pBoothGoods.setGoodsImage(goods.getGoodsImage());
                pBoothGoods.setGoodsName(goods.getGoodsName());
                pBoothGoods.setGoodsPrice(goods.getGoodsPrice());
                pBoothGoods.setRecommondType(recommondType);
                pBoothGoods.setBoothState((byte) 1);
                pBoothGoods.setGoodsSort(sort);
                pBoothGoodsMapper.insertSelective(pBoothGoods);
                ajaxReturnData.setResult(true);
                ajaxReturnData.setMessage("添加成功");
//            }else{
//                ajaxReturnData.setResult(false);
//                ajaxReturnData.setMessage("添加推荐商品不能超过8个");
//            }
        }catch (Exception e){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("系统异常 请稍后再试！");
            throw  new Exception("系统异常"+e);
        }


        return ajaxReturnData;
    }

    @Override
    public PBoothGoods selectRecommondGoodsByPage(PBoothGoods pBoothGoods) {

        List<PBoothGoods> pBoothGoodsList = this.pBoothGoodsMapper.selectRecommondGoodsByPage(pBoothGoods);
        pBoothGoods.setResultList(pBoothGoodsList);
        return pBoothGoods;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PBoothGoods record) {
        pBoothGoodsMapper.updateByPrimaryKeySelective(record);
        return true;
    }

    /**
     * 根据主键删除推荐展位商品
     *
     * @param gid
     * @return
     */
    @Override
    public AjaxReturnData  delRecommondGoods(Integer gid) {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        int i = this.pBoothGoodsMapper.deleteByPrimaryKey(gid);
        if(i>0){
            ajaxReturnData.setType("true");
            ajaxReturnData.setMessage("删除成功");
        }else{
            ajaxReturnData.setType("false");
            ajaxReturnData.setMessage("删除失败，请联系系统管理员");
        }
        return ajaxReturnData;
    }

    /**
     * 管理员审核推荐商品
     *
     * @param recommendId 推荐ID
     * @param verifyType  1 通过 2 不通过
     * @param reason 不通过原因
     * @return
     */
    @Override
    public AjaxReturnData verifyRecommendGoods(Integer recommendId, Integer verifyType, String reason) throws Exception {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        try{
            PBoothGoods pBoothGoods = new PBoothGoods();
            //判断使用站内信来通知商户审核结果 reason
            if(verifyType==1){
                pBoothGoods.setBoothState((byte) 1);
            }else{
                pBoothGoods.setBoothState((byte) 2);
            }
            pBoothGoods.setBoothGoodsId(recommendId);

            pBoothGoodsMapper.updateByPrimaryKeySelective(pBoothGoods);
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("操作成功");
            ajaxReturnData.setUrl("operation/recommendList");
        }catch (Exception e){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("操作失败");
            ajaxReturnData.setUrl("operation/recommendList");
            throw new Exception("操作失败"+e);
        }

        return ajaxReturnData;
    }
}
